package config

import (
	"gopkg.in/yaml.v2"
	"os"
)

type AppConfig struct {
	OffSet   *OffSet   `yaml:"offset"`
	LogFile  *LogFile  `yaml:"file"`
	Queue    *Queue    `yaml:"queue"`
	Es       *Es       `yaml:"es"`
	Flush    *Flush    `yaml:"flush"`
	Instance *Instance `yaml:"instance"`
}

type OffSet struct {
	OffsetMod  int    `default:"2" yaml:"mod"`
	OffsetLine int64  `default:"0" yaml:"line"`
	OffsetTime string `yaml:"time"`
}

type LogFile struct {
	Path string `yaml:"path"`
}

type Queue struct {
	Size int `default:"16" yaml:"size"`
}

type Es struct {
	EsAddr     string `yaml:"addr"`
	EsPreName  string `default:"slow_log_" yaml:"preName"`
	EsSniff    bool   `default:"false" yaml:"sniff"`
	EsUsername string `yaml:"username"`
	EsPassword string `yaml:"password"`
}

type Flush struct {
	FLushMaxSize     int `default:"50" yaml:"maxSize"`
	FLushMaxInterval int `default:"30" yaml:"maxInterval"`
}

type Instance struct {
	Name string `default:"default" yaml:"name"`
}

func (appConfig *AppConfig) Load(filename string) error {

	//读取文件
	file, err := os.ReadFile(filename)
	if err != nil {
		panic(err)
	}

	//解析yaml中的变量
	err = yaml.Unmarshal(file, appConfig)
	if err != nil {
		return err
	}
	return nil
}
